package com.shuiyun.log.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuiyun.log.anno.LogAnno;
import com.shuiyun.log.dao.UserDAO;
import com.shuiyun.log.enums.LogType;
import com.shuiyun.log.po.UserPO;
import com.shuiyun.log.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: LogDemo
 * @description: 用户service
 * @author: gujun
 * @create: 2019-10-11 09:31
 **/
@Service
public class UserService extends ServiceImpl<UserDAO,UserPO> {

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserService userService;

    public UserVO get(Long id){
        UserVO userVO = new UserVO();
        UserPO userPO = userDAO.selectById(id);
        BeanUtil.copyProperties(userPO,userVO);
        return userVO;
    }

    @LogAnno(dataType = "user")
    @Transactional
    public UserVO save(UserVO userVO){
        if (userVO.getUserName().equals("张三")){
            UserVO lisi = new UserVO();
            lisi.setId(3L);
            lisi.setUserAge(11);
            userService.update(lisi);
        }
        UserPO userPO = new UserPO();
        BeanUtil.copyProperties(userVO,userPO);
        userDAO.insert(userPO);
        return this.get(userPO.getId());
    }

    @LogAnno(dataType = "user",logType = LogType.UPDATE)
    @Transactional
    public UserVO update(UserVO userVO){
        UserPO userPO = new UserPO();
        BeanUtil.copyProperties(userVO,userPO);
        userDAO.updateById(userPO);
        return this.get(userVO.getId());
    }

}
